<!doctype html>
<html lang="en">
 <head> 
  <meta charset="UTF-8"> 
  <meta name="description" content="燎火的个人博客"> 
  <meta name="keyword" content="燎火,qq799378666,软件开发,java,c#,js,html,html5,css"> 
  <title>spring-boot 基础入门</title> 
  <script src="/js/jquery-1.11.1.min.js"></script> 
  <script src="/js/bootstrap-3.3.5/js/bootstrap.js"></script> 
  <link href="/js/bootstrap-3.3.5/css/bootstrap.css" rel="stylesheet"> 
  <script src="/js/base.js"></script> 
  <script src="/js/nav.js"></script> 
  <script src="/js/jquery.toTop.min.js"></script> 
  <link href="/style/article.css" rel="stylesheet"> 
 </head> 
 <body> 
  <div id="main"> 
   <h3 id="title">spring-boot 基础入门</h3> 
   <div id="content">
    <h3 style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: 18px; font-weight: 500; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">过去我们web服务端框架配置一般是SpringMVC+Spring+Mybatis,最近因为准备做一个第三方平台接口,写一个restful 风格的http api,所以学习了spring-boot,这里讲一下一些基础的配置</h3>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">------初探</p>
    <blockquote style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; position: relative; font-size: 1.5rem; background: rgb(244, 244, 244); margin: 20px 0px; padding: 16px 24px 16px 35px; font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal;">
     <ul style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-left: 30px; padding: 8px 15px;" class=" list-paddingleft-2">
      <li><p>The recommended way to get started using spring-boot in your project is with a dependency management system – the snippet below can be copied and pasted into your build. Need help? See our getting started guides on building with Maven and Gradle.</p><pre class="hljs xml" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">&lt;parent&gt;
&nbsp;&nbsp;&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&nbsp;&nbsp;&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&nbsp;&nbsp;&lt;version&gt;1.4.3.RELEASE&lt;/version&gt;&lt;/parent&gt;&lt;dependencies&gt;
&nbsp;&nbsp;&lt;dependency&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&nbsp;&nbsp;&lt;/dependency&gt;&lt;/dependencies&gt;</pre></li>
     </ul>
    </blockquote>
    <pre class="hljs less" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">package&nbsp;hello;import&nbsp;org.springframework.boot.*;import&nbsp;org.springframework.boot.autoconfigure.*;import&nbsp;org.springframework.stereotype.*;import&nbsp;org.springframework.web.bind.annotation.*;@Controller@EnableAutoConfigurationpublic&nbsp;class&nbsp;SampleController&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;@RequestMapping("/")&nbsp;&nbsp;&nbsp;&nbsp;@ResponseBody
&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;home()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;"Hello&nbsp;World!";
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;throws&nbsp;Exception&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SpringApplication.run(SampleController.class,&nbsp;args);
&nbsp;&nbsp;&nbsp;&nbsp;}
}</pre>
    <h5 style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: 12px; font-weight: 500; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">上都是官网资料(在慢慢习惯看英文文档了0.0),直接启动main方法,spring-boot 内置了一个tomcat,这里版本默认是tomcat8.0,端口为8080,所以我们jdk最好也用1.8,浏览器访问 127.0.0.1:8080,会看到 Hello World!</h5>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">------ 集成mybatis</p>
    <blockquote style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; position: relative; font-size: 1.5rem; background: rgb(244, 244, 244); margin: 20px 0px; padding: 16px 24px 16px 35px; font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal;">
     <ul style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-left: 30px; padding: 8px 15px;" class=" list-paddingleft-2">
      <li><p>与Mybatis的集成,需要加入的dependency如下,然后编写从dao－service－controller的mvc_pattern 文件。这里注意下MappRunApplication类的包的位置,如果放到与controller,service,controller层同一目录会导致注解无法被扫描到。</p></li>
     </ul>
    </blockquote>
    <pre class="hljs xml" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">&nbsp;&lt;dependency&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;scope&gt;test&lt;/scope&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/dependency&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;dependency&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;org.mybatis.spring.boot&lt;/groupId&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;mybatis-spring-boot-starter&lt;/artifactId&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;version&gt;1.1.1&lt;/version&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/dependency&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;dependency&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;mysql&lt;/groupId&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;version&gt;5.1.21&lt;/version&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/dependency&gt;</pre>
    <pre class="hljs less" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">package&nbsp;com.mywebapp.mapper;import&nbsp;org.apache.ibatis.annotations.Mapper;import&nbsp;org.apache.ibatis.annotations.Select;import&nbsp;org.springframework.stereotype.Repository;import&nbsp;java.util.List;import&nbsp;java.util.Map;/**
&nbsp;*&nbsp;Created&nbsp;by&nbsp;gaorui&nbsp;on&nbsp;17/1/12.
&nbsp;*/@Mapper@Repositorypublic&nbsp;interface&nbsp;UserMapper&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;@Select("SELECT&nbsp;*&nbsp;FROM&nbsp;user")
&nbsp;&nbsp;&nbsp;&nbsp;List&lt;Map&lt;String,Object&gt;&gt;&nbsp;getUsers();



}</pre>
    <pre class="hljs swift" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">package&nbsp;com.mywebapp.service.imp;import&nbsp;com.mywebapp.mapper.UserMapper;import&nbsp;com.mywebapp.service.UserService;import&nbsp;org.springframework.beans.factory.annotation.Autowired;import&nbsp;org.springframework.stereotype.Service;import&nbsp;java.util.List;import&nbsp;java.util.Map;/**
&nbsp;*&nbsp;Created&nbsp;by&nbsp;gaorui&nbsp;on&nbsp;17/1/11.
&nbsp;*/@Servicepublic&nbsp;class&nbsp;UserServiceImp&nbsp;implements&nbsp;UserService&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;@Autowired
&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;UserMapper&nbsp;userMapper;&nbsp;&nbsp;&nbsp;&nbsp;@Override
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;List&lt;Map&lt;String,Object&gt;&gt;&nbsp;user()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;&nbsp;userMapper.getUsers();
&nbsp;&nbsp;&nbsp;&nbsp;}


}package&nbsp;com.mywebapp.service;import&nbsp;java.util.List;import&nbsp;java.util.Map;/**
&nbsp;*&nbsp;Created&nbsp;by&nbsp;gaorui&nbsp;on&nbsp;17/1/11.
&nbsp;*/public&nbsp;interface&nbsp;UserService&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;List&lt;Map&lt;String,Object&gt;&gt;&nbsp;user();
}</pre>
    <pre class="hljs css" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">package&nbsp;com.mywebapp.apicontroller;import&nbsp;com.mywebapp.service.UserService;import&nbsp;org.springframework.beans.factory.annotation.Autowired;import&nbsp;org.springframework.web.bind.annotation.*;import&nbsp;java.util.List;import&nbsp;java.util.Map;/**
&nbsp;*&nbsp;Created&nbsp;by&nbsp;gaorui&nbsp;on&nbsp;17/1/11.
&nbsp;*/@RestControllerpublic&nbsp;class&nbsp;UserController&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;@Autowired
&nbsp;&nbsp;&nbsp;&nbsp;UserService&nbsp;userService;&nbsp;&nbsp;&nbsp;&nbsp;@RequestMapping(value&nbsp;=&nbsp;"/users",&nbsp;method&nbsp;=&nbsp;RequestMethod.GET)&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;List&lt;Map&lt;String,Object&gt;&gt;&nbsp;users()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;userService.user();
&nbsp;&nbsp;&nbsp;&nbsp;}
}</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">package com.mywebapp;</p>
    <pre class="hljs swift" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">import&nbsp;org.springframework.boot.SpringApplication;import&nbsp;org.springframework.boot.autoconfigure.SpringBootApplication;/**
&nbsp;*&nbsp;Created&nbsp;by&nbsp;gaorui&nbsp;on&nbsp;17/1/11.
&nbsp;*/@SpringBootApplicationpublic&nbsp;class&nbsp;MappRunApplication&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;throws&nbsp;Exception&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SpringApplication.run(MappRunApplication.class,&nbsp;args);
&nbsp;&nbsp;&nbsp;&nbsp;}
}</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">application.properties 数据库配置文件,自动识别加载 spring.datasource.url=jdbc:mysql://localhost:3306/api_server spring.datasource.username=root spring.datasource.password=yeyuan0110 spring.datasource.driver-class-name=com.mysql.jdbc.Driver ------ 总结 其实我们搭建完成可以发现几乎是零xml配置,并且pom文件里配置文件更简洁了,框架往上级又进一步封装了,所以这里我还是建议大家需要先去看下spring再来玩下这个,因为这些框架越来牛b了,其实也是把双刃剑,让我们更加远离了一些底层原理的东西。 最后附上github项目地址:https://github.com/javagaorui5944/ProxyIpApi</p>
    <p><br></p>
   </div> 
  </div>  
 </body>
</html>